const { body } = require("express-validator");
const { pageno, pagesize, mongoid } = require("../common");
// 用户名
const username = body("username")
    .trim()
    .notEmpty()
    .withMessage("用户名不能为空")
    .bail()
    .isLength({ min: 3, max: 20 })
    .withMessage("用户名长度在3到20个字符之间");

// 密码
const password = body("password")
    .trim()
    .notEmpty()
    .withMessage("密码不能为空")
    .bail()
    .isLength({ min: 6 })
    .custom((value, { req }) => {
        if (!/[A-Z]/.test(value))
            throw new Error("密码必须包含至少一个大写字母");
        if (!/[0-9]/.test(value)) throw new Error("密码必须包含至少一个数字");
        return true;
    });

// 重复密码
const repeatpwd = body("repeatpwd")
    .trim()
    .custom((value, { req }) => {
        if (!value) throw new Error("请输入确认密码");
        if (value !== req.body.password) {
            throw new Error("两次密码不一致");
        }
        return true;
    });
const avatar = body("avatar").trim().notEmpty().withMessage("请上传头像");

module.exports = {
    register: [username, password, repeatpwd, avatar],
    login: [username, password],
    list: [pageno, pagesize],
    delete: [mongoid],
    detail: [mongoid]
};
